package drds.data_migrate.util;


import drds.common.$;
import drds.common.Author;
import drds.data_migrate.Authors;
import drds.data_migrate.vo.ConnectionInfo;
import drds.data_migrate.vo.JobConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.List;

@Author(name = Authors.LI_YANG)
@Slf4j
public final class Reads
{

    /**
     * 对配置进行初步处理：
     * <ol>
     * <li>处理同一个数据库配置了多个jdbcUrl的情况</li>
     * <li>对 tableName 模式，确定分表个数，并处理 table_column_name_list 转 *事项</li>
     * </ol>
     */
    public static void doPretreatment(JobConfig jobConfig) throws SQLException
    {


        getTable(jobConfig);

        checkColumnNameList(jobConfig);
    }

    private static void getTable(JobConfig jobConfig)
    {


        List<ConnectionInfo> connectionInfoList = jobConfig.getConnectionInfoList();


        int tableNum = 0;

        for (int i = 0, size = connectionInfoList.size(); i < size; i++)
        {
            ConnectionInfo connectionInfo = connectionInfoList.get(i);


            {
                // tableName 方式
                // 对每一个connection 上配置的table 项进行解析(已对表名称进行了 ` 处理的)
                List<String> tableNameList = connectionInfo.getTableNameList();


                tableNum += tableNameList.size();

                //taskConfig.set(String.format("%s[%d].%s",
                //      Constant.conn_mark, i, Key.tableNameList), tableNameList);
            }
        }

        jobConfig.setTableNumberMark(tableNum);
    }

    private static void checkColumnNameList(JobConfig jobConfig) throws SQLException
    {

        List<String> userConfiguredColumnNameList = jobConfig.getColumnNameList();

        {
            if (!$.isHasData(userConfiguredColumnNameList))
            {
                throw new IllegalArgumentException("未配置读取数据库表的列信息");
            } else
            {


                {
                    String jdbcUrl = jobConfig.getJdbcUrl();
                    String username = jobConfig.getUsername();
                    String password = jobConfig.getPassword();
                    //
                    String tableName = jobConfig.getTableName();

                    List<String> columnNameList = Jdbcs.getColumnNameList(jdbcUrl, username, password, tableName);
                    for (String columnName : columnNameList)
                    {
                        if (!columnNameList.contains(columnName))
                        {
                            throw new NullPointerException();
                        }
                    }
                    jobConfig.setColumnNamesString(StringUtils.join(userConfiguredColumnNameList, ","));
                    //
                    String splitColumnName = jobConfig.getSplitColumnName();
                    if (!columnNameList.contains(splitColumnName))
                    {
                        throw new RuntimeException("您的配置文件中的列配置信息有误. 因为根据您的配置，您读取的数据库表:%s 中没有主键名为:%s. 请检查您的配置并作出修改." + tableName + splitColumnName);
                    }

                }
            }
        }

    }

}
